home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / PrefSearch.cpp < prev    next >
C/C++ Source or Header  |  2000-06-24  |  10KB  |  430 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. // PrefSearch.cpp : implementation file
  30. //
  31.  
  32. #include "stdafx.h"
  33. #include "Gnucleus.h"
  34. #include "GnucleusDoc.h"
  35. #include "PrefSearch.h"
  36.  
  37. #include "IPFilter.h"
  38.  
  39.  
  40. #ifdef _DEBUG
  41. #define new DEBUG_NEW
  42. #undef THIS_FILE
  43. static char THIS_FILE[] = __FILE__;
  44. #endif
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CPrefSearch property page
  48.  
  49. IMPLEMENT_DYNCREATE(CPrefSearch, CPropertyPage)
  50.  
  51. CPrefSearch::CPrefSearch() : CPropertyPage(CPrefSearch::IDD)
  52. {
  53.     Doc = NULL;
  54.  
  55.     //{{AFX_DATA_INIT(CPrefSearch)
  56.     //}}AFX_DATA_INIT
  57. }
  58.  
  59. CPrefSearch::~CPrefSearch()
  60. {
  61. }
  62.  
  63. void CPrefSearch::DoDataExchange(CDataExchange* pDX)
  64. {
  65.     CPropertyPage::DoDataExchange(pDX);
  66.     //{{AFX_DATA_MAP(CPrefSearch)
  67.     DDX_Control(pDX, IDC_EDIT_SEARCH, m_ebSearch);
  68.     DDX_Control(pDX, IDC_EDIT_NODE, m_ebNode);
  69.     DDX_Control(pDX, IDC_EDIT_REPLY, m_ebReply);
  70.     DDX_Control(pDX, IDC_CHECK_REPLY, m_chkReply);
  71.     DDX_Control(pDX, IDC_COMBO_SEARCH, m_cmbSearch);
  72.     DDX_Control(pDX, IDC_COMBO_NODE, m_cmbNode);
  73.     DDX_Control(pDX, IDC_LIST_SEARCH, m_lstSearch);
  74.     DDX_Control(pDX, IDC_LIST_NODE, m_lstNode);
  75.     //}}AFX_DATA_MAP
  76. }
  77.  
  78.  
  79. BEGIN_MESSAGE_MAP(CPrefSearch, CPropertyPage)
  80.     //{{AFX_MSG_MAP(CPrefSearch)
  81.     ON_BN_CLICKED(IDC_CHECK_REPLY, OnCheckReply)
  82.     ON_EN_CHANGE(IDC_EDIT_REPLY, OnChangeEditReply)
  83.     ON_EN_CHANGE(IDC_EDIT_SEARCH, OnChangeEditSearch)
  84.     ON_CBN_EDITCHANGE(IDC_COMBO_SEARCH, OnEditchangeComboSearch)
  85.     ON_BN_CLICKED(IDC_BUTTON_SEARCH_REMOVE, OnButtonSearchRemove)
  86.     ON_BN_CLICKED(IDC_BUTTON_SEARCH_ADD, OnButtonSearchAdd)
  87.     ON_EN_CHANGE(IDC_EDIT_NODE, OnChangeEditNode)
  88.     ON_CBN_EDITCHANGE(IDC_COMBO_NODE, OnEditchangeComboNode)
  89.     ON_BN_CLICKED(IDC_BUTTON_NODE_REMOVE, OnButtonNodeRemove)
  90.     ON_BN_CLICKED(IDC_BUTTON_NODE_ADD, OnButtonNodeAdd)
  91.     //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CPrefSearch message handlers
  96.  
  97. BOOL CPrefSearch::OnInitDialog() 
  98. {
  99.     CPropertyPage::OnInitDialog();
  100.     
  101.     Doc = (CGnucleusDoc *) ((CGnucleusApp *) AfxGetApp())->MasterDoc;
  102.  
  103.     CRect rect;
  104.  
  105.     // Setup search list
  106.     int offSet;
  107.  
  108.     if(m_lstSearch.GetScrollLimit(SB_VERT))
  109.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  110.     else
  111.         offSet = 4;
  112.  
  113.     m_lstSearch.GetWindowRect(&rect);
  114.  
  115.     m_lstSearch.InsertColumn(0, "File Type", LVCFMT_LEFT,
  116.         (rect.Width() - offSet) * 2/3, 0);
  117.     m_lstSearch.InsertColumn(1, "Rule", LVCFMT_LEFT,
  118.         (rect.Width() - offSet) * 1/3, 1);
  119.  
  120.     m_lstSearch.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  121.     
  122.     // Setup node list
  123.     if(m_lstNode.GetScrollLimit(SB_VERT))
  124.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  125.     else
  126.         offSet = 4;
  127.  
  128.     m_lstNode.GetWindowRect(&rect);
  129.  
  130.     m_lstNode.InsertColumn(0, "IP", LVCFMT_LEFT,
  131.         (rect.Width() - offSet) * 2/3, 0);
  132.     m_lstNode.InsertColumn(1, "Rule", LVCFMT_LEFT,
  133.         (rect.Width() - offSet) * 1/3, 1);
  134.  
  135.     m_lstNode.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  136.  
  137.     // Add items to the list boxes
  138.     m_cmbSearch.AddString("Allow");
  139.     m_cmbSearch.AddString("Deny");
  140.     m_cmbSearch.SetCurSel(0);
  141.  
  142.     m_cmbNode.AddString("Allow");
  143.     m_cmbNode.AddString("Deny");
  144.     m_cmbNode.SetCurSel(0);
  145.  
  146.     // Set Reply to queries
  147.     if(Doc->m_MaxReplies)
  148.     {
  149.         m_chkReply.SetCheck(1);
  150.         m_ebReply.SetWindowText( DWrdtoStr(Doc->m_MaxReplies) );
  151.     }
  152.     else
  153.     {
  154.         m_ebReply.SetWindowText("Unlim.");
  155.         m_ebReply.EnableWindow(FALSE);
  156.     }
  157.  
  158.     // Fill search filter list
  159.     std::vector<BlockedSearch>::iterator itBlock;
  160.  
  161.     for(itBlock = Doc->SearchFilter.begin(); itBlock != Doc->SearchFilter.end(); itBlock++)
  162.     {
  163.         int row = m_lstSearch.GetItemCount();
  164.  
  165.         m_lstSearch.InsertItem(row, (*itBlock).Name);
  166.  
  167.         if( (*itBlock).Permis == 'A')
  168.             m_lstSearch.SetItemText(row, 1, "Allow");
  169.         else
  170.             m_lstSearch.SetItemText(row, 1, "Deny");
  171.     }
  172.  
  173.     m_lstSearch.GetWindowRect(&rect);
  174.  
  175.     if(m_lstSearch.GetScrollLimit(SB_VERT))
  176.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  177.     else
  178.         offSet = 4;
  179.  
  180.     m_lstSearch.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  181.     m_lstSearch.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  182.     
  183.     // Fill filter list with items
  184.     std::vector<CString> FilterList;
  185.     CIPFilter::GetConfigStrings (FilterList);
  186.  
  187.     std::vector<CString>::iterator itIP;
  188.  
  189.     for(itIP = FilterList.begin(); itIP != FilterList.end(); itIP++)
  190.     {
  191.         int row = m_lstNode.GetItemCount();
  192.  
  193.         CString Filter(*itIP);
  194.         Filter.Replace("-1", "*");
  195.         Filter.Replace(':', '.');
  196.  
  197.         m_lstNode.InsertItem(row, Filter.Mid(2));
  198.  
  199.         if(Filter.GetAt(0) == 'A')
  200.             m_lstNode.SetItemText(row, 1, "Allow");
  201.         else
  202.             m_lstNode.SetItemText(row, 1, "Deny");
  203.     }
  204.  
  205.     m_lstNode.GetWindowRect(&rect);
  206.  
  207.     if(m_lstNode.GetScrollLimit(SB_VERT))
  208.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  209.     else
  210.         offSet = 4;
  211.  
  212.     m_lstNode.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  213.     m_lstNode.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  214.  
  215.  
  216.     return TRUE;  // return TRUE unless you set the focus to a control
  217.                   // EXCEPTION: OCX Property Pages should return FALSE
  218. }
  219.  
  220. void CPrefSearch::OnCheckReply() 
  221. {
  222.     if(m_chkReply.GetCheck())
  223.     {
  224.         m_ebReply.SetWindowText( DWrdtoStr(Doc->m_MaxReplies) );
  225.         m_ebReply.EnableWindow();
  226.     }
  227.     else
  228.     {
  229.         m_ebReply.SetWindowText("Unlim.");
  230.         m_ebReply.EnableWindow(FALSE);
  231.     }
  232.  
  233.     SetModified();
  234. }
  235.  
  236. void CPrefSearch::OnChangeEditReply() 
  237. {
  238.     SetModified();
  239. }
  240.  
  241. void CPrefSearch::OnChangeEditSearch() 
  242. {
  243.     SetModified();    
  244. }
  245.  
  246. void CPrefSearch::OnEditchangeComboSearch() 
  247. {
  248.     SetModified();
  249. }
  250.  
  251. void CPrefSearch::OnButtonSearchAdd() 
  252. {
  253.     CString newFilter;
  254.  
  255.     m_ebSearch.GetWindowText(newFilter);
  256.  
  257.     if(newFilter == "")
  258.         return;
  259.  
  260.     // Get Allow / Deny permission
  261.     CString Permission;
  262.     m_cmbSearch.GetWindowText(Permission);
  263.  
  264.     int row = m_lstSearch.GetItemCount();
  265.     m_lstSearch.InsertItem(row, newFilter);
  266.     m_lstSearch.SetItemText(row, 1, Permission);
  267.  
  268.     // Resize the columns for scroll bars
  269.     CRect rect;
  270.     m_lstSearch.GetWindowRect(&rect);
  271.  
  272.     int offSet;
  273.     if(m_lstSearch.GetScrollLimit(SB_VERT))
  274.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  275.     else
  276.         offSet = 4;
  277.  
  278.     m_lstSearch.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  279.     m_lstSearch.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  280.     
  281.     SetModified();    
  282. }
  283.  
  284. void CPrefSearch::OnButtonSearchRemove() 
  285. {
  286.     if(m_lstSearch.GetSelectionMark() != -1)
  287.         m_lstSearch.DeleteItem( m_lstSearch.GetSelectionMark() );
  288.  
  289.     // Resize the columns for scroll bars
  290.     CRect rect;
  291.     m_lstSearch.GetWindowRect(&rect);
  292.  
  293.     int offSet;
  294.     if(m_lstSearch.GetScrollLimit(SB_VERT))
  295.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  296.     else
  297.         offSet = 4;
  298.  
  299.     m_lstSearch.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  300.     m_lstSearch.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  301.  
  302.     SetModified();
  303. }
  304.  
  305. void CPrefSearch::OnChangeEditNode() 
  306. {
  307.     SetModified();
  308. }
  309.  
  310. void CPrefSearch::OnEditchangeComboNode() 
  311. {
  312.     SetModified();
  313. }
  314.  
  315. void CPrefSearch::OnButtonNodeAdd() 
  316. {
  317.     CString newFilter;
  318.  
  319.     m_ebNode.GetWindowText(newFilter);
  320.  
  321.     // Check for 3 dots
  322.     int pos = 0;
  323.     for(int i = 0; i < 3; i++)
  324.     {
  325.         pos = newFilter.Find('.', pos) + 1;
  326.  
  327.         if(pos == 0)
  328.             return;
  329.     }
  330.  
  331.     // Get Allow / Deny permission
  332.     CString Permission;
  333.     m_cmbNode.GetWindowText(Permission);
  334.  
  335.     int row = m_lstNode.GetItemCount();
  336.     m_lstNode.InsertItem(row, newFilter);
  337.     m_lstNode.SetItemText(row, 1, Permission);
  338.  
  339.     // Resize the columns for scroll bars
  340.     CRect rect;
  341.     m_lstNode.GetWindowRect(&rect);
  342.  
  343.     int offSet;
  344.     if(m_lstNode.GetScrollLimit(SB_VERT))
  345.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  346.     else
  347.         offSet = 4;
  348.  
  349.     m_lstNode.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  350.     m_lstNode.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  351. }
  352.  
  353. void CPrefSearch::OnButtonNodeRemove() 
  354. {
  355.     if(m_lstNode.GetSelectionMark() != -1)
  356.         m_lstNode.DeleteItem( m_lstNode.GetSelectionMark() );
  357.  
  358.     // Resize the columns for scroll bars
  359.     CRect rect;
  360.     m_lstNode.GetWindowRect(&rect);
  361.  
  362.     int offSet;
  363.     if(m_lstNode.GetScrollLimit(SB_VERT))
  364.         offSet = ::GetSystemMetrics(SM_CXVSCROLL) + 3;
  365.     else
  366.         offSet = 4;
  367.  
  368.     m_lstNode.SetColumnWidth(0, (rect.Width() - offSet) * 2/3);
  369.     m_lstNode.SetColumnWidth(1, (rect.Width() - offSet) * 1/3);
  370.  
  371.     SetModified();
  372. }
  373.  
  374. BOOL CPrefSearch::OnApply() 
  375. {
  376.     CString store;
  377.  
  378.     // Set max reply settings
  379.     if(m_chkReply.GetCheck())
  380.     {
  381.         m_ebReply.GetWindowText(store);
  382.  
  383.         Doc->m_MaxReplies = atoi(store);
  384.     }
  385.     else
  386.         Doc->m_MaxReplies = 0;
  387.     
  388.     // Reload the search filter
  389.     Doc->SearchFilter.clear();
  390.  
  391.     for(int row = 0; row < m_lstSearch.GetItemCount(); row++)
  392.     {
  393.         BlockedSearch Filter;
  394.         
  395.         Filter.Name   = m_lstSearch.GetItemText(row, 0);
  396.         Filter.Permis = m_lstSearch.GetItemText(row, 1).GetAt(0);
  397.         
  398.         Doc->SearchFilter.push_back(Filter);
  399.     }
  400.  
  401.     // Reload the node filter
  402.     CIPFilter::RemoveAll();
  403.  
  404.     for(row = 0; row < m_lstNode.GetItemCount(); row++)
  405.     {
  406.         CString Filter( m_lstNode.GetItemText(row, 1).GetAt(0) );
  407.  
  408.         Filter += ":" + m_lstNode.GetItemText(row, 0);
  409.  
  410.         Filter.Replace("*", "-1");
  411.         Filter.Replace('.', ':');
  412.  
  413.         CIPFilter::AddFilter(Filter);
  414.     }
  415.  
  416.     if(m_lstSearch.GetItemCount() == 0)
  417.     {
  418.         BlockedSearch Search;
  419.         Search.Name = "*.*";
  420.         Search.Permis = 'A';
  421.         Doc->SearchFilter.push_back(Search);
  422.     }
  423.  
  424.     if(m_lstNode.GetItemCount() == 0)
  425.         CIPFilter::AddFilter("A:-1:-1:-1:-1");
  426.  
  427.     return CPropertyPage::OnApply();
  428. }
  429.  
  430.